package com.wanshi.controller;

import com.wanshi.po.*;
import com.wanshi.service.TiService;
import com.wanshi.utils.Constants;
import com.wanshi.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("dati")
public class NormalController {

    @Autowired
    private TiService tiService;


    @RequestMapping("/list")
    public String list(HttpSession session, Model model,int pagenum) {
        User user = (User)session.getAttribute("user");
        Integer user_id = user.getId();
        Page<TimuPo> page = tiService.list( pagenum,user_id);
        model.addAttribute("page",page);
        return "list";
    }

    @RequestMapping("/findById")
    public String findById(Model model,String id) {
        Timu ti = tiService.findById(id);
        model.addAttribute("ti",ti);
        return "da";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result add(HttpServletRequest request, HttpSession session, String url, String method, int degree ) {

        User user = (User)session.getAttribute("user");
        Integer user_id = user.getId();
        String content = request.getParameter("content");
        String answer = request.getParameter("answer");
        Timu timu = new Timu(content, url, method,degree, user_id,answer);
        tiService.add(timu);
        return new Result(Constants.SUCCESS);
    }
}
